import 'package:common_utils/common_utils.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/controllers/seckill_controller/seckill_order_page_controller.dart';
import 'package:xinxuan/http/http_util_service.dart';
import 'package:xinxuan/utils/total_util.dart';
import 'package:xinxuan/vo/seckill_order_detail_vo.dart';
import 'package:xinxuan/vo/seckill_order_list_vo.dart';

class SeckillOrderDetailPageController extends GetxController {

  final CommonPageController commonPageController = Get.find();
  final SeckillOrderPageController seckillOrderPageController = Get.find<SeckillOrderPageController>();

  /// 订单详情数据
  var seckillOrderDetailData = SeckillOrderDetailData(id: 0, totalAmount: '0', status: 1, orderType: 1).obs;
  /// 同意协议
  var consentAgreement = true.obs;
  /// 倒计时剩余时间
  var duration = 0.obs;
  /// 时间倒计时
  TimerUtil? _timerUtil; 

  // var test = false.obs;

  @override
  void onReady() {
    Map<String, dynamic> argumentsMap = Get.arguments;
    int orderId = argumentsMap['seckillOrderId'];
    getSeckillOrderDetail(orderId);
    super.onReady();
  }
  
  /// 获取订单详情
  getSeckillOrderDetail(int orderId, {bool loading = true}) async {
    SeckillOrderDetailVo seckillOrderDetailVo = await HttpUtilService.getSeckillOrderDetail(orderId: orderId, loading: loading);
    seckillOrderDetailData.value = seckillOrderDetailVo.data!;
    _calculationTime(orderId);
    update();
  }

  /// 计算时间
  _calculationTime(int orderId) {
    // if (!test.value) {
    //   test.value = true;
    //   seckillOrderDetailData.value.status = 1;
    //   seckillOrderDetailData.value.remainingTime = 15;
    // }
    if (seckillOrderDetailData.value.status != 1) {
      return;
    }
    _timerUtil = TimerUtil(mInterval: 1000, mTotalTime: seckillOrderDetailData.value.remainingTime! * 1000);
    _timerUtil!.setOnTimerTickCallback((int tick) {
      duration.value = (tick ~/ 1000);
      if (tick.toInt() <= 0) {
        _timerUtil!.cancel();
        getSeckillOrderDetail(orderId);
      }
    });
    _timerUtil!.startCountDown();
  }

  /// 确认付款
  seckillOrderConfirmPayment() async {
    if (!consentAgreement.value) {
      EasyLoading.showToast('请同意置换委托协议');
      return;
    }
    int orderId = seckillOrderDetailData.value.id!;
    bool res = await HttpUtilService.seckillOrderConfirmPayment(orderId: orderId);
    if (res) {
      TotalUtil.showSuccess('确认付款成功');
      getSeckillOrderDetail(orderId, loading: false);
      commonPageController.getUserInfo(loading: false);
      for (int i = 0; i < seckillOrderPageController.dataArr.length; i++) {
        SeckillOrderListVo item = seckillOrderPageController.dataArr[i];
        item.data!.forEach((goodsItem) {
          if (orderId == goodsItem.id) {
            seckillOrderPageController.getSeckillTabItemOrderList(i);
          }
        });
      }
    }
  }

  /// 确认收款
  seckillOrderConfirmReceipt() async {
    if (!consentAgreement.value) {
      EasyLoading.showToast('请同意置换委托协议');
      return;
    }
    int orderId = seckillOrderDetailData.value.id!;
    bool res = await HttpUtilService.seckillOrderConfirmReceipt(orderId: orderId);
    if (res) {
      TotalUtil.showSuccess('确认收款成功');
      getSeckillOrderDetail(orderId, loading: false);
      commonPageController.getUserInfo(loading: false);
      for (int i = 0; i < seckillOrderPageController.dataArr.length; i++) {
        SeckillOrderListVo item = seckillOrderPageController.dataArr[i];
        item.data!.forEach((goodsItem) {
          if (orderId == goodsItem.id) {
            seckillOrderPageController.getSeckillTabItemOrderList(i);
          }
        });
      }
    }
  }

  @override
  void onClose() {
    _timerUtil?.cancel();
    super.onClose();
  }
  

}